<?php

namespace App\Http\Controllers;

use App\Http\Requests\BaiduGetLongitudeAndLatitudeRequest;
use App\Http\Requests\BaiduIndustryRequest;
use App\Http\Requests\BaiduKeywordRequest;
use App\Http\Requests\BaiduNearBySearchRequest;
use App\Http\Requests\GeoGetLongitudeAndLatitudeRequest;
use App\Http\Requests\GeoIndustryRequest;
use App\Http\Requests\GeoInputTipsRequest;
use App\Http\Requests\GeoKeywordRequest;
use App\Http\Requests\GeoLocationToAddressRequest;
use App\Http\Requests\GeoNearBySearchRequest;
use App\Http\Requests\TencentGetLongitudeAndLatitudeRequest;
use App\Http\Requests\TencentIndustryRequest;
use App\Http\Requests\TencentKeywordRequest;
use App\Http\Requests\TencentNearBySearchRequest;
use App\Services\KeywordSearchService;
use App\Services\MerchantService;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\DB;
use Jiannei\Response\Laravel\Support\Facades\Response;

class MerchantController extends Controller
{
    /**
     * Geo Keyword
     * @param GeoKeywordRequest $request
     * @return JsonResponse
     */
    public function geoKeyword(GeoKeywordRequest $request): JsonResponse
    {
        try {
            $data = DB::transaction(function () use ($request) {
                $params = $request->only(['page', 'per_page', 'province', 'city', 'district', 'keyword']);
                $keywordParams = [
                    'map' => '高德',
                    'category' => '关键词获客',
                    'province' => $params['province'],
                    'city' => $params['city'],
                    'district' => $params['district'],
                    'keyword' => $params['keyword'],
                ];

                $keywordSearch = KeywordSearchService::getKeywordSearch(
                    $keywordParams['map'],
                    $keywordParams['category'],
                    $keywordParams['province'],
                    $keywordParams['city'],
                    $keywordParams['district'],
                    $keywordParams['keyword']
                );
                if (!$keywordSearch) {
                    $keywordSearch = KeywordSearchService::storeKeywordSearch($keywordParams);
                }

                $url = 'https://restapi.amap.com/v3/place/text?key=' . config('common.geo_key') . '&keywords=' . $params['keyword'] . '&city=' . $params['city'] . '&offset=' . $params['per_page'] . '&page=' . $params['page'] . '&extensions=base';
                if (!str_ends_with($params['district'], '区')) {
                    $url = 'https://restapi.amap.com/v3/place/text?key=' . config('common.geo_key') . '&keywords=' . $params['keyword'] . '&city=' . $params['district'] . '&offset=' . $params['per_page'] . '&page=' . $params['page'] . '&extensions=base';
                }

                $data = MerchantService::getList($params['page'], $params['per_page'], $keywordSearch->id);
                if (count($data->items()) > 0) {
                    $data = $data->items();
                } else {
                    $data = $this->handleData($url, $keywordSearch->id);
                }
                return $data;
            });
            return Response::success($data);
        } catch (\Exception $e) {
            return Response::fail($e->getMessage());
        }
    }

    /**
     * Geo NearBy Search
     * @param GeoNearBySearchRequest $request
     * @return JsonResponse
     */
    public function geoNearBySearch(GeoNearBySearchRequest $request): JsonResponse
    {
        try {
            $data = DB::transaction(function () use ($request) {
                $params = $request->only(['page', 'per_page', 'province', 'city', 'district', 'address', 'radius', 'keyword']);

                $keywordParams = [
                    'map' => '高德',
                    'category' => '附近获客',
                    'province' => $params['province'],
                    'city' => $params['city'],
                    'district' => $params['district'],
                    'address' => $params['address'],
                    'radius' => $params['radius'],
                    'keyword' => $params['keyword']
                ];

                $keywordSearch = KeywordSearchService::getKeywordSearch1(
                    $keywordParams['map'],
                    $keywordParams['category'],
                    $keywordParams['province'],
                    $keywordParams['city'],
                    $keywordParams['district'],
                    $keywordParams['address'],
                    $keywordParams['radius'],
                    $keywordParams['keyword']
                );
                if (!$keywordSearch) {
                    $keywordSearch = KeywordSearchService::storeKeywordSearch($keywordParams);
                }

                $data = MerchantService::getList($params['page'], $params['per_page'], $keywordSearch->id);

                if (count($data->items()) > 0) {
                    $data = $data->items();
                } else {
                    $url = 'https://restapi.amap.com/v3/geocode/geo?key=' . config('common.geo_key') . '&address=' . $params['province'] . $params['city'] . $params['district'] . $params['address'];
                    $res = json_decode(file_get_contents($url));
                    $location = null;
                    if ($res->status == '1' && $res->info == 'OK' && count($res->geocodes) > 0) {
                        $location = $res->geocodes[0]->location;
                    }

                    if (!$location) {
                        throw new \Exception('地址转换失败');
                    }

                    $url = 'https://restapi.amap.com/v3/place/around?key=' . config('common.geo_key') . '&location=' . $location . '&keywords=' . $params['keyword'] . '&radius=' . $params['radius'] . '&offset=' . $params['per_page'] . '&page=' . $params['page'] . '&extensions=base';

                    $data = $this->handleData($url, $keywordSearch->id);
                }
                return $data;
            });
            return Response::success($data);
        } catch (\Exception $e) {
            return Response::fail($e->getMessage());
        }
    }

    /**
     * Geo Keyword
     * @param GeoIndustryRequest $request
     * @return JsonResponse
     */
    public function geoIndustry(GeoIndustryRequest $request): JsonResponse
    {
        try {
            $data = DB::transaction(function () use ($request) {
                $params = $request->only(['page', 'per_page', 'province', 'city', 'district', 'industry']);
                $keywordParams = [
                    'map' => '高德',
                    'category' => '行业获客',
                    'province' => $params['province'],
                    'city' => $params['city'],
                    'district' => $params['district'],
                    'keyword' => $params['industry'],
                ];

                $keywordSearch = KeywordSearchService::getKeywordSearch(
                    $keywordParams['map'],
                    $keywordParams['category'],
                    $keywordParams['province'],
                    $keywordParams['city'],
                    $keywordParams['district'],
                    $keywordParams['keyword']
                );
                if (!$keywordSearch) {
                    $keywordSearch = KeywordSearchService::storeKeywordSearch($keywordParams);
                }

                $url = 'https://restapi.amap.com/v3/place/text?key=' . config('common.geo_key') . '&keywords=' . $params['industry'] . '&city=' . $params['city'] . '&offset=' . $params['per_page'] . '&page=' . $params['page'] . '&extensions=base';
                if (!str_ends_with($params['district'], '区')) {
                    $url = 'https://restapi.amap.com/v3/place/text?key=' . config('common.geo_key') . '&keywords=' . $params['industry'] . '&city=' . $params['district'] . '&offset=' . $params['per_page'] . '&page=' . $params['page'] . '&extensions=base';
                }

                $data = MerchantService::getList($params['page'], $params['per_page'], $keywordSearch->id);
                if (count($data->items()) > 0) {
                    $data = $data->items();
                } else {
                    $data = $this->handleData($url, $keywordSearch->id);
                }
                return $data;
            });
            return Response::success($data);
        } catch (\Exception $e) {
            return Response::fail($e->getMessage());
        }
    }

    /**
     * Geo get Longitude And Latitude
     * @param GeoGetLongitudeAndLatitudeRequest $request
     * @return JsonResponse
     */
    public function geoGetLongitudeAndLatitude(GeoGetLongitudeAndLatitudeRequest $request): JsonResponse
    {
        try {
            $params = $request->only(['province', 'city', 'district', 'address']);
            $url = 'https://restapi.amap.com/v3/geocode/geo?key=' . config('common.geo_key') . '&address=' . $params['province'] . $params['city'] . $params['district'] . $params['address'];
            $res = json_decode(file_get_contents($url));
            $location = null;
            if ($res->status == '1' && $res->info == 'OK' && count($res->geocodes) > 0) {
                $location = $res->geocodes[0]->location;
            }

            if (!$location) {
                throw new \Exception('地址转换失败');
            }
            list($location, $latitude) = explode(',', $location);
            $data = [
                'longitude' => $location,
                'latitude' => $latitude
            ];
            return Response::success($data);
        } catch (\Exception $e) {
            return Response::fail($e->getMessage());
        }
    }

    /**
     * Geo Input Tips
     * @param GeoInputTipsRequest $request
     * @return JsonResponse
     */
    public function geoInputTips(GeoInputTipsRequest $request): JsonResponse
    {
        try {
            $params = $request->only(['keyword']);
            $url = 'https://restapi.amap.com/v3/assistant/inputtips?key=' . config('common.geo_key') . '&keywords= ' . $params['keyword'] . '&datatype=poi';
            $res = json_decode(file_get_contents($url));
            $data = [];
            if ($res->status == '1' && $res->info == 'OK') {
                $data = $res->tips;
            }
            return Response::success($data);
        } catch (\Exception $e) {
            return Response::fail($e->getMessage());
        }
    }

    /**
     * Geo Location To Address
     * @param GeoLocationToAddressRequest $request
     * @return JsonResponse
     */
    public function geoLocationToAddress(GeoLocationToAddressRequest $request): JsonResponse
    {
        try {
            $params = $request->only('location');
            $url = 'https://restapi.amap.com/v3/geocode/regeo?key=' . config('common.geo_key') . '&location='. $params['location'] . '&extensions=all';
            $res = json_decode(file_get_contents($url));
            $data = [];
            if ($res->status == '1' && $res->info == 'OK') {
                $data = $res->regeocode;
            }
            return Response::success($data);
        } catch (\Exception $e) {
            return Response::fail($e->getMessage());
        }
    }

    /**
     * Tencent Keyword
     * @param TencentKeywordRequest $request
     * @return JsonResponse
     */
    public function tencentKeyword(TencentKeywordRequest $request): JsonResponse
    {
        try {
            $data = DB::transaction(function () use ($request) {
                $params = $request->only(['page', 'per_page', 'province', 'city', 'district', 'keyword']);
                $keywordParams = [
                    'map' => '腾讯',
                    'category' => '关键词获客',
                    'province' => $params['province'],
                    'city' => $params['city'],
                    'district' => $params['district'],
                    'keyword' => $params['keyword'],
                ];

                $keywordSearch = KeywordSearchService::getKeywordSearch(
                    $keywordParams['map'],
                    $keywordParams['category'],
                    $keywordParams['province'],
                    $keywordParams['city'],
                    $keywordParams['district'],
                    $keywordParams['keyword']
                );

                if (!$keywordSearch) {
                    $keywordSearch = KeywordSearchService::storeKeywordSearch($keywordParams);
                }

                $url = 'https://apis.map.qq.com/ws/place/v1/search?key=' . config('common.tencent_key') . '&keyword=' . $params['keyword'] . '&boundary=region(' . $params['district'] .',0)&page_size=' . $params['per_page'] . '&page_index=' . $params['page'];

                $data = MerchantService::getList($params['page'], $params['per_page'], $keywordSearch->id);
                if (count($data->items()) > 0) {
                    $data = $data->items();
                } else {
                    $data = $this->handleDataByTencent($url, $keywordSearch->id);
                }
                return $data;
            });
            return Response::success($data);
        } catch (\Exception $e) {
            return Response::fail($e->getMessage());
        }
    }

    /**
     * Tencent Near By Search
     * @param TencentNearBySearchRequest $request
     * @return JsonResponse
     */
    public function tencentNearBySearch(TencentNearBySearchRequest $request): JsonResponse
    {
        try {
            $data = DB::transaction(function () use ($request) {
                $params = $request->only(['page', 'per_page', 'province', 'city', 'district', 'address', 'radius', 'keyword']);
                $keywordParams = [
                    'map' => '腾讯',
                    'category' => '附近获客',
                    'province' => $params['province'],
                    'city' => $params['city'],
                    'district' => $params['district'],
                    'address' => $params['address'],
                    'radius' => $params['radius'],
                    'keyword' => $params['keyword'],
                ];

                $keywordSearch = KeywordSearchService::getKeywordSearch1(
                    $keywordParams['map'],
                    $keywordParams['category'],
                    $keywordParams['province'],
                    $keywordParams['city'],
                    $keywordParams['district'],
                    $keywordParams['address'],
                    $keywordParams['radius'],
                    $keywordParams['keyword']
                );

                if (!$keywordSearch) {
                    $keywordSearch = KeywordSearchService::storeKeywordSearch($keywordParams);
                }

                $data = MerchantService::getList($params['page'], $params['per_page'], $keywordSearch->id);
                if (count($data->items()) > 0) {
                    $data = $data->items();
                } else {
                    $url = 'https://apis.map.qq.com/ws/geocoder/v1?key=' . config('common.tencent_key') . '&address=' . $params['province'] . $params['city'] . $params['district'] . $params['address'];
                    $res = json_decode(file_get_contents($url));
                    $longitude = null;
                    $latitude = null;

                    if ($res->status == 0 && $res->message == 'Success') {
                        $longitude = $res->result->location->lng;
                        $latitude = $res->result->location->lat;
                    }

                    if (!$longitude || !$latitude) {
                        throw new \Exception('地址转换失败');
                    }

                    $url = 'https://apis.map.qq.com/ws/place/v1/search?key=' . config('common.tencent_key') . '&keyword=' . $params['keyword'] . '&boundary=nearby('. $latitude .',' . $longitude .',' . $params['radius'] . ')&page_size=' . $params['per_page'] . '&page_index=' . $params['page'];
                    $data = $this->handleDataByTencent($url, $keywordSearch->id);
                }
                return $data;
            });
            return Response::success($data);
        } catch (\Exception $e) {
            return Response::fail($e->getMessage());
        }
    }

    /**
     * Tencent Industry
     * @param TencentIndustryRequest $request
     * @return JsonResponse
     */
    public function tencentIndustry(TencentIndustryRequest $request): JsonResponse
    {
        try {
            $data = DB::transaction(function () use ($request) {
                $params = $request->only(['page', 'per_page', 'industry', 'province', 'city', 'district']);

                $keywordParams = [
                    'map' => '腾讯',
                    'category' => '行业获客',
                    'province' => $params['province'],
                    'city' => $params['city'],
                    'district' => $params['district'],
                    'keyword' => $params['industry'],
                ];

                $keywordSearch = KeywordSearchService::getKeywordSearch(
                    $keywordParams['map'],
                    $keywordParams['category'],
                    $keywordParams['province'],
                    $keywordParams['city'],
                    $keywordParams['district'],
                    $keywordParams['keyword']
                );

                if (!$keywordSearch) {
                    $keywordSearch = KeywordSearchService::storeKeywordSearch($keywordParams);
                }

                $url = 'https://apis.map.qq.com/ws/place/v1/search?key=' . config('common.tencent_key') . '&keyword=' . $params['industry'] . '&boundary=region(' . $params['district'] .',0)&page_size=' . $params['per_page'] . '&page_index=' . $params['page'];

                $data = MerchantService::getList($params['page'], $params['per_page'], $keywordSearch->id);
                if (count($data->items()) > 0) {
                    $data = $data->items();
                } else {
                    $data = $this->handleDataByTencent($url, $keywordSearch->id);
                }
                return $data;
            });
            return Response::success($data);
        } catch (\Exception $e) {
            return Response::fail($e->getMessage());
        }
    }

    /**
     * Tencent Get Longitude And Latitude
     * @param TencentGetLongitudeAndLatitudeRequest $request
     * @return JsonResponse
     */
    public function tencentGetLongitudeAndLatitude(TencentGetLongitudeAndLatitudeRequest $request): JsonResponse
    {
        try {
            $params = $request->only(['province', 'city', 'district', 'address']);

            $url = 'https://apis.map.qq.com/ws/geocoder/v1?key=' . config('common.tencent_key') . '&address=' . $params['province'] . $params['city'] . $params['district'] . $params['address'];
            $res = json_decode(file_get_contents($url));
            $longitude = null;
            $latitude = null;

            if ($res->status == 0 && $res->message == 'Success') {
                $longitude = $res->result->location->lng;
                $latitude = $res->result->location->lat;
            }

            if (!$longitude || !$latitude) {
                throw new \Exception('地址转换失败');
            }

            $data = [
                'longitude' => $longitude,
                'latitude' => $latitude
            ];

            return Response::success($data);
        } catch (\Exception $e) {
            return Response::fail($e->getMessage());
        }
    }

    /**
     * Baidu Keyword
     * @param BaiduKeywordRequest $request
     * @return JsonResponse
     */
    public function baiduKeyword(BaiduKeywordRequest $request): JsonResponse
    {
        try {
            $data = DB::transaction(function () use ($request) {
                $params = $request->only(['page', 'per_page', 'province', 'city', 'district', 'keyword']);
                $keywordParams = [
                    'map' => '百度',
                    'category' => '关键词获客',
                    'province' => $params['province'],
                    'city' => $params['city'],
                    'district' => $params['district'],
                    'keyword' => $params['keyword'],
                ];

                $keywordSearch = KeywordSearchService::getKeywordSearch(
                    $keywordParams['map'],
                    $keywordParams['category'],
                    $keywordParams['province'],
                    $keywordParams['city'],
                    $keywordParams['district'],
                    $keywordParams['keyword']
                );

                if (!$keywordSearch) {
                    $keywordSearch = KeywordSearchService::storeKeywordSearch($keywordParams);
                }

                $url = 'https://api.map.baidu.com/place/v2/search?query=' . $params['keyword'] . '&region=' . $params['district'] . '&output=json&ak=' . config('common.baidu_key') . '&scope=2&page_size=' . $params['per_page'] . '&page_num=' . $params['page'] - 1;

                $data = MerchantService::getList($params['page'], $params['per_page'], $keywordSearch->id);

                if (count($data->items()) > 0) {
                    $data = $data->items();
                } else {
                    $data = $this->handleDataByBaidu($url, $keywordSearch->id);
                }
                return $data;
            });
            return Response::success($data);
        } catch (\Exception $e) {
            return Response::fail($e->getMessage());
        }
    }

    /**
     * Baidu NearBy Search
     * @param BaiduNearBySearchRequest $request
     * @return JsonResponse
     */
    public function baiduNearBySearch(BaiduNearBySearchRequest $request): JsonResponse
    {
        try {
            $data = DB::transaction(function () use ($request) {
                $params = $request->only(['page', 'per_page', 'province', 'city', 'district', 'address', 'radius', 'keyword']);

                $keywordParams = [
                    'map' => '百度',
                    'category' => '附近获客',
                    'province' => $params['province'],
                    'city' => $params['city'],
                    'district' => $params['district'],
                    'address' => $params['address'],
                    'radius' => $params['radius'],
                    'keyword' => $params['keyword'],
                ];

                $keywordSearch = KeywordSearchService::getKeywordSearch1(
                    $keywordParams['map'],
                    $keywordParams['category'],
                    $keywordParams['province'],
                    $keywordParams['city'],
                    $keywordParams['district'],
                    $keywordParams['address'],
                    $keywordParams['radius'],
                    $keywordParams['keyword']
                );

                if (!$keywordSearch) {
                    $keywordSearch = KeywordSearchService::storeKeywordSearch($keywordParams);
                }

                $data = MerchantService::getList($params['page'], $params['per_page'], $keywordSearch->id);

                if (count($data->items()) > 0) {
                    $data = $data->items();
                } else {
                    $url = 'https://api.map.baidu.com/geocoding/v3/?address=' . $params['province'] . $params['city'] . $params['district'] . $params['address'] . '&output=json&ak=' . config('common.baidu_key');
                    $res = json_decode(file_get_contents($url));
                    $longitude = null;
                    $latitude = null;

                    if ($res->status == 0) {
                        $longitude = $res->result->location->lng;
                        $latitude = $res->result->location->lat;
                    }

                    if (!$longitude || !$latitude) {
                        throw new \Exception('地址转换失败');
                    }

                    $url = 'https://api.map.baidu.com/place/v2/search?query=' . $params['keyword'] .'&location=' . $latitude . ',' . $longitude . '&radius=' . $params['radius'] . '&output=json&ak=' . config('common.baidu_key') . '&scope=2&page_size=' . $params['per_page'] . '&page_num=' . $params['page'] - 1;
                    $data = $this->handleDataByBaidu($url, $keywordSearch->id);
                }
                return $data;
            });
            return Response::success($data);
        } catch (\Exception $e) {
            return Response::fail($e->getMessage());
        }
    }

    /**
     * Baidu Industry
     * @param BaiduIndustryRequest $request
     * @return JsonResponse
     */
    public function baiduIndustry(BaiduIndustryRequest $request): JsonResponse
    {
        try {
            $data = DB::transaction(function () use ($request) {
                $params = $request->only(['page', 'per_page', 'province', 'city', 'district', 'industry']);

                $keywordParams = [
                    'map' => '百度',
                    'category' => '行业获客',
                    'province' => $params['province'],
                    'city' => $params['city'],
                    'district' => $params['district'],
                    'keyword' => $params['industry'],
                ];

                $keywordSearch = KeywordSearchService::getKeywordSearch(
                    $keywordParams['map'],
                    $keywordParams['category'],
                    $keywordParams['province'],
                    $keywordParams['city'],
                    $keywordParams['district'],
                    $keywordParams['keyword']
                );

                if (!$keywordSearch) {
                    $keywordSearch = KeywordSearchService::storeKeywordSearch($keywordParams);
                }

                $url = 'https://api.map.baidu.com/place/v2/search?query=' . $params['industry'] . '&region=' . $params['district'] . '&output=json&ak=' . config('common.baidu_key') . '&scope=2&page_size=' . $params['per_page'] . '&page_num=' . $params['page'] - 1;

                $data = MerchantService::getList($params['page'], $params['per_page'], $keywordSearch->id);

                if (count($data->items()) > 0) {
                    $data = $data->items();
                } else {
                    $data = $this->handleDataByBaidu($url, $keywordSearch->id);
                }
                return $data;
            });
            return Response::success($data);
        } catch (\Exception $e) {
            return Response::fail($e->getMessage());
        }
    }

    /**
     * Baidu Get Longitude And Latitude
     * @param BaiduGetLongitudeAndLatitudeRequest $request
     * @return JsonResponse
     */
    public function baiduGetLongitudeAndLatitude(BaiduGetLongitudeAndLatitudeRequest $request): JsonResponse
    {
        try {
            $params = $request->only(['province', 'city', 'district', 'address']);

            $url = 'https://api.map.baidu.com/geocoding/v3/?address=' . $params['province'] . $params['city'] . $params['district'] . $params['address'] . '&output=json&ak=' . config('common.baidu_key');
            $res = json_decode(file_get_contents($url));
            $longitude = null;
            $latitude = null;

            if ($res->status == 0) {
                $longitude = $res->result->location->lng;
                $latitude = $res->result->location->lat;
            }

            if (!$longitude || !$latitude) {
                throw new \Exception('地址转换失败');
            }

            $data = [
                'longitude' => $longitude,
                'latitude' => $latitude
            ];
            return Response::success($data);
        } catch (\Exception $e) {
            return Response::fail($e->getMessage());
        }
    }

    /**
     * Get random merchant
     * @return JsonResponse
     */
    public function randomMerchant(): JsonResponse
    {
        try {
            $data = MerchantService::randomMerchant();
            return Response::success($data);
        } catch (\Exception $e) {
            return Response::fail($e->getMessage());
        }
    }

    /**
     * Handle data
     * @param string $url
     * @param int $keywordSearchId
     * @return array
     */
    private function handleData(string $url, int $keywordSearchId): array
    {
        $data = [];
        $res = json_decode(file_get_contents($url));
        if ($res->status == '1' && $res->info == 'OK' && count($res->pois) > 0) {
            foreach ($res->pois as $v) {
                list($longitude, $latitude) = explode(',', $v->location);
                $merchantParams = [
                    'keyword_search_id' => $keywordSearchId,
                    'map' => '高德',
                    'title' => $v->name,
                    'category' => $v->type,
                    'tel' => empty($v->tel) ? '' : $v->tel,
                    'address' => empty($v->address) ? '' : $v->address,
                    'province' => $v->pname,
                    'city' => $v->cityname,
                    'district' => $v->adname,
                    'longitude' => $longitude,
                    'latitude' => $latitude,
                ];

                $merchant = MerchantService::getMerchant(
                    $merchantParams['map'],
                    $merchantParams['title'],
                    $merchantParams['address'],
                    $merchantParams['province'],
                    $merchantParams['city'],
                    $merchantParams['district']
                );

                $data[] = $merchantParams;
                if (!$merchant) {
                    MerchantService::storeMerchant($merchantParams);
                }
            }
        }
        return $data;
    }

    /**
     * @param string $url
     * @param int $keywordSearchId
     * @return array
     */
    private function handleDataByTencent(string $url, int $keywordSearchId): array
    {
        $data = [];
        $res = json_decode(file_get_contents($url));
        if ($res->status == 0 && $res->message == "Success" && count($res->data) > 0) {
            foreach ($res->data as $v) {
                $merchantParams = [
                    'keyword_search_id' => $keywordSearchId,
                    'map' => '腾讯',
                    'title' => $v->title,
                    'category' => $v->category,
                    'tel' => $v->tel,
                    'address' => $v->address,
                    'province' => $v->ad_info->province,
                    'city' => $v->ad_info->city,
                    'district' => $v->ad_info->district,
                    'longitude' => $v->location->lng,
                    'latitude' => $v->location->lat,
                ];

                $merchant = MerchantService::getMerchant(
                    $merchantParams['map'],
                    $merchantParams['title'],
                    $merchantParams['address'],
                    $merchantParams['province'],
                    $merchantParams['city'],
                    $merchantParams['district']
                );

                $data[] = $merchantParams;
                if (!$merchant) {
                    MerchantService::storeMerchant($merchantParams);
                }
            }
        }
        return $data;
    }

    /**
     * Handle data
     * @param string $url
     * @param int $keywordSearchId
     * @return array
     */
    private function handleDataByBaidu(string $url, int $keywordSearchId): array
    {
        $data = [];
        $res = json_decode(file_get_contents($url));

        if ($res->status == 0 && $res->message == 'ok' && count($res->results) > 0) {
            foreach ($res->results as $v) {
                $merchantParams = [
                    'keyword_search_id' => $keywordSearchId,
                    'map' => '百度',
                    'title' => $v->name,
                    'category' => $v->detail_info->tag,
                    'tel' => $v->telephone ?? '',
                    'address' => $v->address,
                    'province' => $v->province,
                    'city' => $v->city,
                    'district' => $v->area,
                    'longitude' => $v->location->lng,
                    'latitude' => $v->location->lat,
                ];

                $merchant = MerchantService::getMerchant(
                    $merchantParams['map'],
                    $merchantParams['title'],
                    $merchantParams['address'],
                    $merchantParams['province'],
                    $merchantParams['city'],
                    $merchantParams['district']
                );

                $data[] = $merchantParams;
                if (!$merchant) {
                    MerchantService::storeMerchant($merchantParams);
                }
            }
        }
        return $data;
    }
}
